<template>
  <div style="background-color: var(--el-bg-color); margin-left: 12px">
    <h4 style="padding: 6px; margin-bottom: 0" v-if="instance.title">
      {{ instance.title }}
      <span class="textbutton refresh" @click="refresh">
        <el-icon>
          <Refresh />
        </el-icon>
      </span>
    </h4>
    <ElEmpty v-if="instance.list.length === 0"></ElEmpty>
    <template v-for="(row, index) in instance.list" :key="index">
      <div class="item">
        <div class="tag" v-if="!row.readed"></div>
        <ElImage class="avatar" :src="row.LastMessage.User.fullUrl" alt="头像"></ElImage>
        <div class="center" @click="onRead(row)">
          <div class="title">
            {{ row.LastMessage.User.nickname }}
            <span :style="'color:' + instance.titles[row.type].color">
              {{ instance.titles[row.type].text }}</span>
          </div>
          <div class="message">
            {{
              instance.types[row.LastMessage.type]?.formatter(
                row.LastMessage.msg,
              )
            }}
          </div>
        </div>
        <div class="title" style="text-align: right">
          <bDelete text="是否删除会话" @onconfirm="() => instance.delete(index)"><el-icon>
              <Delete />
            </el-icon></bDelete>
          <div>{{ row.LastMessage.create }}</div>
        </div>
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
import { GroupList } from "@b/model/base/group";
import { ElEmpty, ElIcon, ElImage } from "element-plus";
import { Delete, Refresh } from "@element-plus/icons-vue";
import bDelete from "@/components/widget/b_delete.vue";
import { useRouter } from "vue-router";
import type { interGroupInfo } from "@b/model/base/type";
import { NotiStore } from "@b/model/store/notification";
interface prop {
  instance: GroupList;
  reAbout?: boolean;
}

const { instance, reAbout } = defineProps<prop>();
const router = useRouter();
const emits = defineEmits<{
  (e: "about", item: interGroupInfo): void;
}>();
if (instance.immediateCheck) {
  instance.refresh(1);
}
const refresh = () => {
  instance.refresh(1);
  new NotiStore().getGroupList();
}
const onRead = (row: interGroupInfo) => {
  if (reAbout) {
    emits("about", row);
  } else {
    const { groupName, type } = row;
    if (type === "C2C") router.push("C2CList?id=" + groupName);
  }
};
</script>
<style scoped>
.refresh {
  padding: 14px;
  padding-bottom: 0;
  font-size: 0.85em;
}

.item {
  position: relative;
  display: flex;
  margin: 8px;
  padding: 8px;
  padding-bottom: 10px;
  align-items: center;
  border-bottom: 1px solid var(--el-border-color);
}

.item .avatar {
  width: 40px;
  min-width: 40px;
  height: 40px;
  margin-right: 12px;
  border-radius: 12px;
}

.item .center {
  flex-grow: 1;
  cursor: pointer;
  overflow: hidden;
  white-space: nowrap;
  margin-right: 4px;
}

.item .title {
  color: var(--el-text-color-secondary);
  font-size: 0.75em;
  min-width: 5em;
}

.item .message {
  margin-top: 4px;
  font-size: 0.8rem;
}

.tag {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--el-color-primary);
  position: absolute;
  top: 8px;
  right: 28px;
}
</style>
